// pages/contentDetails/contentDetails.js
const app = getApp();
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
var qqmapsdk;
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    istop: false,
    countActivityNum: '',
    show: false,
    req: false,
    typeName: '',
    active: 0,
    pageEnd: false,
    param: {
      pageNumber: 1,
      pageSize: 10
    },
    list: [],
    isSend: false,
    content: '',
    iptShow: false,
    isSend: false,
    keyheight: 0,
    loginShow: false,
    login: false,
    phone: false,
    orgOut: "",
    ismodal: false,
    isOrg: false,
    isSee: false,
    willShare: false,
    optionState: "",
    // isCollect:false,
    remark: "写评论...",
    share: "",
    getOrgState: null,
    refresh: true,
  },
  check: function() {
    this.toggle("show", true);
  },
  onClose: function() {
    this.toggle("show", false);
  },
  contact: function(e) {
    // console.log(e);
  },
  org(e) {
    var dataset = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/pages/orgHome/orgHome?id=' + dataset.orgid
    })
  },
  toggleToast(e) {
    // console.log(e.detail)
    if (!e.detail.loginShow) {
      this.toggle('loginShow', false)
    }
    this.onShow()
  },
  toleaveInfo() {
    // if (this.data.detail.status == 0 || this.data.detail.messageType == 3){
    //   return
    // }
    if (this.data.istop && this.data.detail.status != 0 && this.data.detail.messageType != 3) {
      // console.log('滚动成功dd')
      if (this.data.active == 1) {
        this.setData({
          active: 0,
        })
        this.toggle("param.pageNumber", 1);
        this.toggle("pageEnd", false);
        this.toggle("list", []);
        this.list()
      } else {
        this.setData({
          istop: false
        })
        wx.pageScrollTo({
          scrollTop: 0,
          duration: 300,
          success: function(res) {
            // console.log('滚动成功')
          }
        })
      }
    } else if (!this.data.istop && this.data.detail.status != 0 && this.data.detail.messageType != 3) {
      // console.log('滚动成功22')
      wx.pageScrollTo({
        selector: "#messagebox",
        duration: 300,
        success: function(res) {
          // console.log('滚动成功')
        },
        complete: function(res) {
          // console.log(res)
        }
      })
      this.setData({
        istop: true,
        active: 0,
      })
      this.toggle("param.pageNumber", 1);
      this.toggle("pageEnd", false);
      this.toggle("list", []);
      this.list()
    }
  },
  toUserCard: function(e) {
    var id = e.currentTarget.dataset.id;
    var name = e.currentTarget.dataset.name;
    wx.navigateTo({
      url: '/pages/myCardNew/myCardNew?type=visitor&id=' + id + '&name=' + name,
    })
  },
  onChange(e) {
    var that = this
    if (e.currentTarget.dataset.index != that.data.active && that.data.detail) {
      that.toggle('active', e.currentTarget.dataset.index)
      that.toggle("param.pageNumber", 1);
      that.toggle("pageEnd", false);
      that.toggle("list", []);
      that.list()
    }
  },
  // 删除评论
  delComment(e) {
    var that = this
    var dataset = e.currentTarget.dataset;
    if (that.data.type) {
      var url = 'orgmini/home/removeComment'
      var data = {
        "messageId": dataset.messageid,
        "id": dataset.id,
        "type": that.data.type
      }
    } else {
      var url = 'orgmini/notice/removeComment'
      var data = {
        "messageId": dataset.messageid,
        "id": dataset.id
      }
    }
    wx.showModal({
      title: '提示',
      content: '您确定要删除当前评论吗?',
      confirmColor: '#2168EB',
      success(res) {
        if (res.confirm) {
          app.req({
            url: url,
            data: data,
            success: function(res) {

              if (200 == res.code) {
                var list = that.data.list.filter(function(item) {
                  return item.id != dataset.id
                })
                that.toggle('list', list)
                wx.showToast({
                  title: '删除成功'
                })
              } else {
                wx.showToast({
                  title: res.msg,
                  icon: 'none'
                })
              }
            }
          });
        }
      }
    })
  },
  // 收藏 取消收藏
  collect() {
    if (!this.data.login) {
      this.login();
      return
    }
    if (this.data.detail.status == 0) {
      return
    }
    var that = this;
    if (that.data.detail.isCollection) {
      var url = 'orgmini/removeCollections'
      var tip = '取消收藏成功'
    } else {
      var url = 'orgmini/addCollections'
      var tip = '收藏成功'
    }
    app.req({
      url: url,
      data: {
        type: that.data.detail.messageType,
        collectionId: that.data.detail.id
      },
      success: function(res) {
        if (200 == res.code) {
          that.toggle("detail.isCollection", !that.data.detail.isCollection)
          wx.showToast({
            title: tip,
            icon: 'none'
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
      }
    });
  },
  // 打开文档
  openFile(e) {
    var dataset = e.currentTarget.dataset;
    // console.log(dataset.url);
    wx.downloadFile({
      url: dataset.url,
      success: function(res) {
        var filePath = res.tempFilePath
        // console.log(filePath);
        wx.openDocument({
          filePath: filePath,
          success: function(res) {}
        })
      }
    })
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  toPage(e) {
    if (!this.data.login) {
      this.login();
      return
    }
    if (this.data.detail.messageType == 3) {
      return
    }
    this.toggle('refresh', true);
    var dataset = e.currentTarget.dataset;
    var url = dataset.turn
    if (dataset.id && dataset.messageid) {
      if (dataset.type) {
        url = url + '?id=' + dataset.id + '&messageId=' + dataset.messageid + '&type=' + dataset.type
      } else {
        url = url + '?id=' + dataset.id + '&messageId=' + dataset.messageid
      }

    } else if (dataset.id && dataset.type) {
      url = url + '?id=' + dataset.id + '&type=' + dataset.type
    } else if (dataset.id) {
      url = url + '?id=' + dataset.id
    }
    if (this.data.detail.messageType == 5) {
      url += '&origin=home';
    }
    wx.navigateTo({
      url: url
    })
    // console.log(url)
  },
  list: function(append) {
    var that = this;
    that.toggle("req", false);
    var tabActive = that.data.active;
    if (that.data.type) {
      if (0 == tabActive) {
        var url = "orgmini/home/infoCommentList";
        that.toggle("param.type", 2);
      } else if (1 == tabActive) {
        var url = "orgmini/home/seePeopleList"
      }
    } else {
      if (0 == tabActive) {
        var url = "orgmini/notice/infoComment"
      } else if (1 == tabActive) {
        var url = "orgmini/notice/infoUser"
      }
    }
    var param = that.data.param
    app.req({
      url: url,
      data: param,
      success: function(res) {
        if (200 == res.code) {
          console.log("登录状态", that.data.login)
          if (!that.data.login) {
            if (that.data.getSeeState == 1) {
              that.login();
            }
          }
          var list = res.data.rows;
          var year = new Date().getFullYear();
          var month = new Date().getMonth() + 1;
          var day = new Date().getDate();
          // console.log(list)
          for (var item of list) {
            if (tabActive == 0) {
              // 创建时间
              if (year == item.createTime.substring(0, 4)) {
                if (month == item.createTime.substring(5, 7) && day == item.createTime.substring(8, 10)) {
                  item.createTime = item.createTime.substring(11, 16);
                } else {
                  item.createTime = item.createTime.substring(5, 7) + '月' + item.createTime.substring(8, 10) + '日 ' + item.createTime.substring(11, 16);
                }
              } else {
                item.createTime = item.createTime.substring(0, 4) + '年' + item.createTime.substring(5, 7) + '月' + item.createTime.substring(8, 10) + '日 ' + item.createTime.substring(11, 16);
              }
              // 删除时间
              if (year == item.deleteTime.substring(0, 4)) {
                if (month == item.deleteTime.substring(5, 7) && day == item.deleteTime.substring(8, 10)) {
                  item.deleteTime = item.deleteTime.substring(11, 16);
                } else {
                  item.deleteTime = item.deleteTime.substring(5, 7) + '月' + item.deleteTime.substring(8, 10) + '日 ' + item.deleteTime.substring(11, 16);
                }
              } else {
                item.deleteTime = item.deleteTime.substring(0, 4) + '年' + item.deleteTime.substring(5, 7) + '月' + item.deleteTime.substring(8, 10) + '日 ' + item.deleteTime.substring(11, 16);
              }
            } //已查看
            else {
              that.toggle('detail.seeNumber', res.data.total)
              that.toggle('countActivityNum', res.data.countActivityNum)
            }

          }
          if (list.orgCommentsDtoList) {
            if (list.orgCommentsDtoList.length > 2) {
              var arr = [];
              arr.push(list.orgCommentsDtoList[0])
              arr.push(list.orgCommentsDtoList[1])
              list.orgCommentsDtoList = arr
            }
          }
          if (0 == tabActive) {
            for (var item of list) {
              item.piclist = []
              for (var j of item.picUrlList) {
                item.piclist.push(j.imgUrl)
              }
              item.commentTotle = item.orgCommentsDtoList.length
              if (item.orgCommentsDtoList.length > 2) {
                item.orgCommentsDtoList = [item.orgCommentsDtoList[0], item.orgCommentsDtoList[1]]
              }
            }
          }

          that.toggle("list", append ? that.data.list.concat(list) : list);
          that.toggle("req", true);
          if (!list || (that.data.list.length >= res.data.total) || that.data.active == 1 && !res.data.rows.length) {
            that.toggle("pageEnd", true);
          }
        }
      }
    });
  },
  // 推荐详情与详情
  detail: function(append) {
    var that = this;
    if (this.data.origin == 'homeBusiness'){
      var url = 'orgmini/business/activityDetail';
      var param = {
        id: that.data.id,
      }
    }else{
      var url = 'orgmini/notice/info'
      var param = {
        orgId: that.data.orgId,
        id: that.data.id,
        sourceType: 1
      }
    }
    app.req({
      url: url,
      data: param,
      success: function(res) {
        if (200 == res.code) {
          if (that.data.orgId) {
            that.toggle("getOrgState", res.data.isOrg);
            that.toggle("getSeeState", res.data.isOnlyOrg);
            if (!that.data.login) {
              if (res.data.isOnlyOrg == 1) {
                that.login();
              }
            }
          }
          if (res.data.delFlag) {
            that.toggle("optionState", "恢复");
          }
          var detail = res.data;
          if (detail.createTime) {
            detail.createTime = detail.createTime.substring(0, 16);
          }
          let reg = new RegExp('tp=webp', 'g')
          detail.content = detail.content.replace(reg, '')
          // var content = that.escape2Html(detail.content)
          // var content1 = content.replace(/\<img/gi, '<img style="max-width:100%;height:auto" ')
          var rich = detail.content
          that.toggle('rich', rich)

          if (detail.messageType == 5) {
            that.toggle('origin', "home")
            wx.hideShareMenu();
            // for (var i = 0; i < list.length; i++) {
            // let index = i;
            if (detail.imgCollection.length == 1) {
              wx.getImageInfo({
                src: detail.imgCollection[0],
                complete: (res) => {
                  var scale = res.width / res.height;
                  var height = parseFloat(500 / scale).toFixed(2);
                  detail.height = height;
                  if (scale > 1) {
                    detail.width = 1
                  } else {
                    detail.width = 0
                  }
                  that.toggle("detail", detail);
                }
              })
            }
            // }
          }
          that.toggle("detail", detail);
          if (detail.messageType == 3) {
            that.toggle("remark", "暂不支持评论");
          }
          if (detail.status != 2 || detail.recommendStatus != 2) {
            that.toggle("share", "share");
          }
          if (that.data.origin != 'homeBusiness'){
            var title = detail.messageType == 1 ? "资讯" : (detail.messageType == 3 || detail.messageType == 15 ? "通知" : (detail.messageType == 2 ? "资源" : (detail.messageType == 4 ? "党建" : (detail.messageType == 5 ? "" : "推荐"))))
            that.toggle("typeName", title);
          }else{
            var title = '商机'
          }
          if (that.data.typeName == '通知') {
            that.toggle('active', 1)
          }
          if (detail.messageType == 1) {
            wx.setNavigationBarTitle({
              title: title + "内容"
            })
          } else {
            wx.setNavigationBarTitle({
              title: title + "详情"
            })
          }
          // 已彻底删除
          if (res.data.delFlag == 2) {
            var pages = getCurrentPages();
            if (pages.length > 1) {
              var prevPage = pages[pages.length - 2];
              if (prevPage.route == "pages/resourceList/resourceList") {} else {
                wx.showModal({
                  title: '',
                  content: '此' + title + '已被彻底删除',
                  showCancel: false,
                  success(res) {
                    if (res.confirm) {
                      wx.switchTab({
                        url: '/pages/newIndex/newIndex'
                      })
                    }
                  }
                })
              }
            }

          }
          if (res.data.isOrg == 3 && res.data.isSee == 0) {
            that.toggle('isOrg', true)
          } else if (res.data.isOrg == 0 && res.data.isSee == 0) {
            that.toggle('isSee', true)
          } else {
            that.toggle('isSee', false)
            that.toggle('isOrg', false)
          }
          WxParse.wxParse('rich', 'md', rich, that, 5);
          that.list();
        } else {
          wx.hideToast({
            success() {
              wx.showToast({
                title: res.msg,
                duration: 5000,
                icon: 'none'
              })
            }
          })

        }
      }
    });
  },
  //地点
  realOpenGPS() {
    var that = this;
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userLocation']) {
          wx.getLocation({
            type: 'gcj02',
            success: function(res) {
              var latitude = res.latitude
              var longitude = res.longitude
              qqmapsdk.reverseGeocoder({
                location: {
                  latitude: latitude,
                  longitude: longitude
                },
                success: function(res) {
                  console.log('获取当前地址', res)
                  if (res.status == 0 && res.result.address) {
                    var add = res.result.address_component
                    var address = add.province + ' ' + add.city + ' ' + add.district + ' ' + add.street_number
                    app.req({
                      url: "orgmini/notice/browse",
                      data: {
                        messageId: that.data.id,
                        sourceType: 1,
                        location: address,
                        latitude: latitude,
                        longitude: longitude,
                      },
                      success: function(res) {

                      }
                    })
                  }
                },
                fail: function(res) {
                  console.log(res);
                },
              });
            },
            fail(res) {
              console.log(res)
            }
          });
        } else {
          app.req({
            url: "orgmini/notice/browse",
            data: {
              messageId: that.data.id,
              sourceType: 1,
              location: '',
              latitude: '',
              longitude: '',
            },
            success: function(res) {

            }
          })
        }
      }
    })
  },
  escape2Html: function(str) {
    var arrEntities = {
      'lt': '<',
      'gt': '>',
      'nbsp': ' ',
      'amp': '&',
      'quot': '"'
    };
    return str.replace(/&(lt|gt|nbsp|amp|quot);/ig, function(all, t) {
      return arrEntities[t];
    });
  },

  login() {
    // console.log("是否可查看getSeeState",this.data.getSeeState)
    if (!this.data.login) {
      this.toggle('loginShow', true)
      this.selectComponent('#login').showClose()
    }
  },
  //弹窗
  deal(e) {
    if (e.currentTarget.dataset.type == "1") {
      var pages = getCurrentPages();
      var url = '/pages/orgHome/orgHome?id=' + this.data.orgId
      wx.redirectTo({
        url: '/pages/orgHome/orgHome?id=' + this.data.orgId
      })
      // console.log(url)
    } else if (e.currentTarget.dataset.type == "0") {
      wx.switchTab({
        url: '/pages/newIndex/newIndex'
      })
    } else if (e.currentTarget.dataset.type == "3") {
      wx.switchTab({
        url: '/pages/index/index?tabActive=1'
      })
    }
    this.setData({
      ismodal: false
    })
  },
  preventTouchMove() {},

  // 组织删除
  orgDelete(e) {
    if (!this.data.login) {
      this.login();
      return
    }
    var that = this;
    var type = Boolean(e.currentTarget.dataset.type * 1);
    var tip = "";
    if (that.data.detail.delFlag && type) {
      tip = "请确认是否恢复";
    } else if (that.data.detail.delFlag && !type) {
      tip = "请确认是否彻底删除";
    } else if (!that.data.detail.delFlag) {
      tip = "请确认是否删除";
    }
    var url = "";
    if (that.data.detail.delFlag && type) {
      url = "orgmini/activity/orgRestoreAct";
    } else if (that.data.detail.delFlag && !type) {
      url = "orgmini/activity/orgCompleteAct";
    } else if (!that.data.detail.delFlag) {
      url = "orgmini/activity/orgDeleteAct";
    }
    var title = "";
    if (that.data.detail.delFlag && type) {
      title = "恢复成功";
    } else if (that.data.detail.delFlag && !type) {
      title = "彻底删除成功";
    } else if (!that.data.detail.delFlag) {
      title = "删除成功";
    }
    if (this.data.origin == "home") {
      this.toggle("detail.messageType", 7)
    }
    if (this.data.origin == "homeBusiness"){
      var url = 'orgmini/business/deleteActivity';
      var data = {
        "id": that.data.detail.id,
      }
    }else{
      var data = {
        "orgId": that.data.detail.orgId,
        "type": that.data.detail.delFlag ? "" : that.data.detail.messageType,
        "relationId": that.data.detail.id,
      }
    }
    wx.showModal({
      title: '提示',
      content: tip,
      confirmText: "确认",
      success(res) {
        if (res.confirm) {
          app.req({
            url: url,
            data: data,
            success: function(res) {
              if (200 == res.code) {
                that.detail();
                wx.showToast({
                  title: title,
                  icon: 'none'
                });
                // that.toggle("isMore",false);
                if (that.data.detail.delFlag && !type || that.data.origin == "home" || that.data.origin == "homeBusiness") {
                  var pages = getCurrentPages();
                  // console.log(pages)
                  if (pages.length > 1) {
                    var prevPage = pages[pages.length - 2];
                    if (prevPage.route == "pages/newIndex/newIndex") {
                      prevPage.setData({
                        refresh: true
                      })
                    }
                    // console.log(prevPage)
                    wx.navigateBack();
                  } else {
                    wx.switchTab({
                      url: '/pages/newIndex/newIndex',
                    })
                  }
                }
              }
            }
          })
        } else if (res.cancel) {}
      }
    })
  },

  // 输入框显示
  toComment: function(e) {
    var dataset = e.currentTarget.dataset;
    var that = this;
    if (dataset.turn) {
      var origin = "";
      if (this.data.detail.messageType == 5) {
        origin = "home";
      }
      if (dataset.type) {
        wx.navigateTo({
          url: '/pages/replyDeatils/replyDeatils?id=' + dataset.id + '&messageId=' + that.data.id + '&type=' + dataset.type + '&origin=' + origin,
        })
      } else {
        wx.navigateTo({
          url: '/pages/replyDeatils/replyDeatils?id=' + dataset.id + '&messageId=' + that.data.id + '&origin=' + origin,
        })
      }
    } else {
      this.toggle('iptPlaceholder', dataset.name)
      this.toggle('iptShow', true)
      this.toggle('parentId', dataset.id)
    }


  },
  bindblur: function() {
    var that = this
    setTimeout(function() {
      that.toggle('iptShow', false)
    }, 300)
  },
  bindinput: function(e) {
    this.toggle('content', e.detail.value)
    this.widthCheck(e.detail.value, 1000)
    if (this.data.content != '') {
      this.toggle('isSend', true)
    } else {
      this.toggle('isSend', false)
    }
  },
  bindfocus: function(e) {
    // console.log(e.detail.height + 'keyboard')
    this.toggle('keyheight', e.detail.height)
    if (e.detail.value == '') {
      this.toggle('isSend', false)
    }
  },
  toSend: function() {
    var that = this;

    if (that.data.content != '' && !that.data.content.match(/^[ ]*$/)) {
      if (that.data.content != '') {
        var n = 0
        // console.log('词库2', this.data.dirty.length)
        for (var i = 0; i < this.data.dirty.length; i++) {
          // console.log('循环')
          if (that.data.content.indexOf(this.data.dirty[i]) >= 0) {
            // console.log('查询2')
            wx.showToast({
              title: '您输入的内容有违禁词，敏感词已被替换成星号,请编辑后重新提交',
              icon: 'none',
              duration: 2000
            })
            var fw = this.data.dirty[i]
            var fw1 = ''
            for (var k = 0; k < fw.length; k++) {
              fw1 = fw1 + '*'
            }
            var activityProcess = that.data.content.replace(fw, fw1)
            that.setData({
              content: activityProcess
            });
            // console.log('替换', that.data.content)
            // console.log('替换1', activityProcess)
            n = n + 1
          }
        }
        if (n > 0) {
          return
        }
      }
      if (that.data.type) {
        var url = "orgmini/home/notice/addNewComment"
        var data = {
          "messageId": that.data.id,
          "content": that.data.content,
          "parentId": that.data.parentId,
          "type": 2
        }
      } else {
        var url = "orgmini/notice/saveComment"
        var data = {
          "messageId": that.data.id,
          "content": that.data.content,
          "parentId": that.data.parentId
        }
      }
      app.req({
        url: url,
        method: "post",
        data: data,
        success: function(res) {
          // console.log(res)
          that.list()
          wx.showToast({
            title: '评论成功',
            icon: 'success',
            duration: 2000
          })

        }
      });
      this.toggle('content', '')
    }
  },
  getdirty() {
    var that = this
    app.req({
      url: "wechat/dirty/all",
      method: "post",
      success: function(res) {
        // console.log('脏字库')
        if (res.code == 200) {
          wx.setStorageSync('dirty', res.data);
          that.setData({
            dirty: res.data
          })
        }
      }
    });
  },
  // 输入框长度字节限制
  widthCheck(str, maxLen) {
    var that = this;
    var w = 0;
    var tempCount = 0;
    //length 获取字数数，不区分汉子和英文 
    for (var i = 0; i < str.length; i++) {
      //charCodeAt()获取字符串中某一个字符的编码 
      var c = str.charCodeAt(i);
      //单字节加1 
      if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
        w++;
      } else {
        w += 2;
      }
      if (w > maxLen) {
        str = str.substr(0, i);
        that.toggle('content', str)
        break;
      }
    }
  },
  hidden() {
    if (this.data.detail.status == 1) {
      this.toggle("willShare", true);
    }
  },
  getUserInfo() {
    var that = this
    wx.getSetting({
      success(res) {
        // console.log(res)
        // if (!res.authSetting['scope.userInfo'] || wx.getStorageSync('uid') == '') {
        if (wx.getStorageSync('uid') == '') {
          that.toggle("login", false);
          that.detail()
        } else {
          that.toggle("login", true);
          var phone = wx.getStorageSync("phone")
          if (phone == '') {
            that.toggle("phone", true);
          } else {
            that.toggle("phone", false);
          }
          app.getUserInfo(function(userInfo) {
            that.toggle("uid", userInfo.id);
            if (that.data.refresh) {
              that.detail()
            }
          });
        }
      },
    })
  },
  setMore(e) {
    var dataset = e.currentTarget.dataset;
    if (dataset.type == 'remark') {
      var index = dataset.index;
      var temp_str = 'list[' + index + '].isMore';
      this.setData({
        [temp_str]: true
      })
    } else {
      this.toggle("isMore", true);
    }
  },
  isorghide(e) {
    var dataset = e.currentTarget.dataset;
    if (dataset.type == 'remark') {
      var index = dataset.index;
      var temp_str = 'list[' + index + '].isMore';
      this.setData({
        [temp_str]: false
      })
    } else {
      this.toggle("isMore", false)
    }
  },
  preventTouchMove: function(e) {},
  openImg(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var item = e.currentTarget.dataset.item;
    var remark = e.currentTarget.dataset.remark;
    var imgArr = [];
    if (remark) {
      imgArr = remark;
    } else {
      for (var i = 0; i < item.length; i++) {
        if (this.data.origin == 'homeBusiness'){
          imgArr.push(item[i].url);
        }else{
          imgArr.push(item[i].fileUrl);
        }
      };
    }
    this.toggle("refresh", false);
    wx.previewImage({
      current: imgArr[index], //当前图片地址
      urls: imgArr, //所有要预览的图片的地址集合 数组形式
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  reset() {
    this.toggle('refresh', false);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log("获取当前数据", options)
    if (options.origin && options.origin == 'homeBusiness'){
      this.toggle("origin", options.origin);
    }else{
      qqmapsdk = new QQMapWX({
        key: 'GK5BZ-WXALX-7QC4N-7ZTQN-5VHKQ-5GFLF'
      });
    }
    this.toggle("id", options.id);
    this.toggle("param.messageId", options.id);
    if (options.orgId) {
      this.toggle("orgId", options.orgId);
    }
    if (options.type) {
      this.toggle("type", options.type);
      this.toggle('param.type', options.type)
    }
    if (wx.getStorageSync('dirty') == undefined || wx.getStorageSync('dirty') == '') {
      this.getdirty()
    } else {
      this.setData({
        dirty: wx.getStorageSync('dirty')
      });
    }
    if (!options.origin) {
      this.realOpenGPS()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this
    console.log('刷新刷新')
    that.toggle("param.pageNumber", 1);
    that.toggle('willShare', false)
    that.getUserInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var that = this;
    this.toggle("refresh", true);
    wx.getSetting({
      success(res) {
        console.log(res)
        // if (!res.authSetting['scope.userInfo'] || wx.getStorageSync('uid') == '') {
        if (wx.getStorageSync('uid') == '') {
          that.toggle("param.pageNumber", 1);
          that.toggle("pageEnd", false);
          that.detail()
        } else {
          app.getUserInfo(function(userInfo) {
            that.toggle("param.pageNumber", 1);
            that.toggle("pageEnd", false);
            that.detail()
          });
        }
      },
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (!this.data.pageEnd) {
      this.toggle("param.pageNumber", this.data.param.pageNumber + 1);
      this.list(true);
    } 
    // else if (!(this.data.detail.status != 1 || (this.data.detail.isRecommend == 1 && this.data.detail.recommendStatus != 1)) && this.data.list.length) {
    //   wx.showToast({
    //     title: '已经全部加载完毕',
    //     icon: "none"
    //   })
    // }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(res) {
    var that = this;
    this.toggle("willShare", true);
    app.req({
      url: "orgmini/notice/addMessageShare",
      method: "post",
      data: {
        "messageId": that.data.id,
        "sourceType": 1,
      },
      success: function(res) {}
    });
    return {
      title: (this.data.type ? '' : this.data.typeName + '：') + this.data.detail.newsDigest
    }
  },
  // onShareTimeline() {
  //   return {
  //     title: (this.data.type ? '' : this.data.typeName + '：') + this.data.detail.newsDigest,
  //     query: 'id=' + this.data.id
  //   }
  // },
})